package com.fingard.dsp.bank.directbank.cdrcb01;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.util.DateHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CDRCB01Transaction extends CDRCB01Base implements ITransaction {

	public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
		RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
		String tmpRespCode = "";
		String tmpRespInfo = "";
		retAts.writeStart();// 写返回ATS的统一头部信息
		try {
			Calendar tmpCalendarFrom = Calendar.getInstance();
			tmpCalendarFrom.setTime(p_req7516.getDateFrom());
			Calendar tmpCalendarTo = Calendar.getInstance();
			tmpCalendarTo.setTime(p_req7516.getDateTo());

			int tmpIntDateFrom = 0;
			int tmpIntDateTo = 0;
			while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
				Calendar tmpClndrStepTo = Calendar.getInstance();
				tmpClndrStepTo.setTime(tmpCalendarFrom.getTime());
				// 判断是否跨月
				if (DateHelper.compareMonth(tmpCalendarFrom, tmpCalendarTo) == -1) {
					// 跨月则设置为月末
					tmpClndrStepTo.set(Calendar.DAY_OF_MONTH, tmpClndrStepTo.getActualMaximum(Calendar.DAY_OF_MONTH));
				} else {
					tmpClndrStepTo.setTime(tmpCalendarTo.getTime());
				}

				CDRCB01Header bankHeader = new CDRCB01Header();
				bankHeader.tranCode = "001003";
				XmlTextWriter xmlWriter = new XmlTextWriter();
				xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
				xmlWriter.writeStartElement("BCDL");
				xmlWriter.writeElementString("ACCT_NO", p_req7516.actNum);
				xmlWriter.writeElementString("BEGIN_DATE", Format.Date8Format.format(tmpCalendarFrom.getTime()));
				xmlWriter.writeElementString("END_DATE", Format.Date8Format.format(tmpClndrStepTo.getTime()));
				xmlWriter.writeElementString("BEGIN_POS", "1");
				xmlWriter.writeEndDocument();

				// 测试代码：打印报文
				System.out.println("xmlWriter.toXmlString():\n" + xmlWriter.toXmlString());
				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader, "ALL");
				// 如果返回tmpStrRet[0]表示返回connMsg，connMsg 为空则表示响应正常，
				// String tmpStrRet[] = { "",
				// "00000000|success|20150804160900903#<?xml version=\"1.0\"
				// encoding=\"UTF-8\"
				// ?><BCDL><ACCT_NO>021200000120010006853</ACCT_NO><TOTAL_NUM>5</TOTAL_NUM><CUR_NUM>5</CUR_NUM><LIST><RECORD><TRAN_DATE>20150803</TRAN_DATE><TRAN_FLOW>31466</TRAN_FLOW><TRAN_AMT>688.80</TRAN_AMT><AMT_FLAG>-</AMT_FLAG><BALANCE>2301367.70</BALANCE><REMARK>行内转账</REMARK><TRAN_BRANCH>02120000</TRAN_BRANCH><BRANCH_NAME>成都农商银行武侯支行</BRANCH_NAME><REC_ACC/><REC_NAME/></RECORD>"
				// +
				// "<RECORD><TRAN_DATE>20150711</TRAN_DATE><TRAN_FLOW>32103</TRAN_FLOW><TRAN_AMT>500000.00</TRAN_AMT><AMT_FLAG>+</AMT_FLAG><BALANCE>2301367.70</BALANCE><REMARK>网银对公通知存款支取</REMARK><TRAN_BRANCH>02120000</TRAN_BRANCH><BRANCH_NAME>成都农商银行武侯支行</BRANCH_NAME><REC_ACC/><REC_NAME/></RECORD><RECORD><TRAN_DATE>20150711</TRAN_DATE><TRAN_FLOW>32102</TRAN_FLOW><TRAN_AMT>500000.00</TRAN_AMT><AMT_FLAG>-</AMT_FLAG><BALANCE>1801367.70</BALANCE>"
				// +
				// "<REMARK>网银开通知</REMARK><TRAN_BRANCH>02120000</TRAN_BRANCH><BRANCH_NAME>成都农商银行武侯支行</BRANCH_NAME><REC_ACC/><REC_NAME/></RECORD></LIST></BCDL>"
				// };
				if (tmpStrRet[0].length() == 0) {
					String[] strRet = splitRetToString(tmpStrRet[1]);// 把结果拆分成String数组
					if (strRet[0].equals("00000000")) {
						StringBuilder tmpSbMsg = new StringBuilder();
						XmlTextReader tmpBkReader = loadBankRespXmlReader(strRet[3], tmpSbMsg);
						if (tmpBkReader != null) {
							// 00000000为成功码
							if (tmpBkReader.getFirstText("/BCDL/CUR_NUM").equals("0")) // 返回0表示无记录
							{
								tmpRespCode = "-1";
								tmpRespInfo = "银行返回记录为空！";
								break;
							}
							// 提取各返回节点内容
							while (tmpBkReader.read("/BCDL/LIST/RECORD")) {
								//tmpRespCode = "0";
								tmpRespInfo = "success";
								// 读取信息
								RetAtsTransDetail tmpRetDetail = new RetAtsTransDetail();
								XElement tmpRetNode = tmpBkReader.readedXElement;
								String tran_date = tmpRetNode.getSingleText("TRAN_DATE");
								tmpRetDetail.actNum = p_req7516.actNum;
								tmpRetDetail.tranDate = DateHelper.getFormat12Date(tran_date);
								tmpRetDetail.valueDate = tmpRetDetail.tranDate;// 格式yyyy-MM-dd
								tmpRetDetail.tranTime = tmpRetDetail.tranDate + " " + "00:00:00";
								tmpRetDetail.comment = tmpRetNode.getSingleTextNotNull("REMARK").trim();
								tmpRetDetail.accBal = tmpRetNode.getSingleText("BALANCE");
								tmpRetDetail.recAccNum = tmpRetNode.getSingleTextNotNull("REC_ACC").trim();
								tmpRetDetail.recAccName = tmpRetNode.getSingleTextNotNull("REC_NAME").trim();
								String loanSign = tmpRetNode.getSingleText("AMT_FLAG");// 借贷标志
								String txAmount = tmpRetNode.getSingleText("TRAN_AMT");
								// 银行返回借贷标志 借记为-，贷记为+"，dsp返回资金系统1-借 2贷
								if (loanSign.equals("-")) {
									tmpRetDetail.cdFlag = "1";
									if (txAmount.indexOf("-") >= 0) {
										tmpRetDetail.cdFlag = "2";
									}
								} else {
									tmpRetDetail.cdFlag = "2";
									if (txAmount.indexOf("-") >= 0) {
										tmpRetDetail.cdFlag = "1";
									}
								}
								tmpRetDetail.tranAmt = txAmount.replace("-", "");
								tmpRetDetail.originator = tmpRetNode.getSingleTextNotNull("TRAN_FLOW").trim();

								retAts.writeRetDetail(tmpRetDetail);
							}

						} else {
							tmpRespCode = "-1";
							tmpRespInfo = tmpSbMsg.toString();
						}
					} else {
						// "查询失败"
						tmpRespCode = "-1";
						tmpRespInfo =strRet[1];
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpStrRet[1];
				}
				tmpCalendarFrom = tmpClndrStepTo;
				tmpCalendarFrom.add(Calendar.DAY_OF_MONTH, 1);
				tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
				tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
			}
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		if (tmpRespCode.equals("-1")) {
			retAts = new RetAtsTrans(p_req7516.reqHeader);
			retAts.writeStart();// 写返回ATS的统一头部信息
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		} else if (retAts.getWrittenRdCount() == 0) {
			retAts.writeRespCode("0");
			retAts.writeRespInfo("交易为空");
		}
		retAts.writeEnd();
		return retAts;
	}

	// 当日明细查询调用历史明细查询方法
	public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
		// 通用b2e0035
		Req7516HisTrans tmpReq7516 = new Req7516HisTrans(p_req7515.reqHeader);
		tmpReq7516.actArea = p_req7515.actArea;
		tmpReq7516.actCur = p_req7515.actCur;
		tmpReq7516.actNum = p_req7515.actNum;
		String tmpStrToday = Format.Date8Format.format(new Date());
		tmpReq7516.dateFrom = tmpStrToday;
		tmpReq7516.dateTo = tmpStrToday;
		return d_7516_HisTrans(tmpReq7516);
	}
}
